
package io.dbw.modules.business.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dbw.common.annotation.SysLog;
import io.dbw.common.enums.BusinessTypeEnum;
import io.dbw.common.utils.AssertUtils;
import io.dbw.common.utils.Constant;
import io.dbw.common.utils.PageUtils;
import io.dbw.common.utils.Result;
import io.dbw.modules.business.entity.SysRoleEntity;
import io.dbw.modules.business.entity.SysUserRoleEntity;
import io.dbw.modules.business.service.SysRoleMenuService;
import io.dbw.modules.business.service.SysRoleService;
import io.dbw.modules.business.service.SysUserRoleService;
import io.dbw.modules.business.vo.CommonUserVO;
import io.dbw.modules.business.vo.IdsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * 
 */
@Validated
@RestController
@RequestMapping("/sys/role")
@Api(tags = "角色管理")
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	/**
	 * 角色列表
	 */
	@GetMapping("/list")
	@ApiOperation(value="列表查询")
	@RequiresPermissions("system:role:list")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query",name = "pageNo",value ="第几页",dataType ="int",required = true),
			@ApiImplicitParam(paramType = "query",name = "pageSize",value ="每页条数",dataType ="int",required = true),
			@ApiImplicitParam(paramType = "query",name = "status",value ="状态",dataType ="int")
	})
	public Result<PageUtils<SysRoleEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params){
		//如果不是超级管理员，则只查询自己创建的角色列表
		CommonUserVO user = getUser();
		if(user.getManagerFlag().equals(Constant.SYSTEM_COMMON_Y)){
			return Result.error("不是管理员不能操作");
		}
		PageUtils page = sysRoleService.queryPage(params);
		return writeSuccess(  page);
	}


	/**
	 * 角色信息
	 */
	@GetMapping("/info")
	@ApiOperation(value="根据ID获取信息")
	@RequiresPermissions("system:role:info")
	@ApiImplicitParam(paramType = "query",name = "roleId", value = "角色ID",  dataTypeClass=Long.class,   required = true, example = "1")
	public Result<SysRoleEntity> info(@NotNull(message = "角色ID不能为空") Long roleId){
		SysRoleEntity role = sysRoleService.getById(roleId);
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		return writeSuccess(role);
	}
	
	/**
	 * 保存角色
	 */
	@SysLog(value = "角色",businessType = BusinessTypeEnum.INSERT)
	@PostMapping("/save")
	@RequiresPermissions("system:role:add")
	@ApiOperation(value="保存")
	public Result save(@Validated @RequestBody SysRoleEntity role){
		role.setRoleId(null);
		CommonUserVO user = getUser();
		role.setCreateBy(user.getUserId());
		sysRoleService.saveRole(role);
		return writeSuccess();
	}
	
	@SysLog(value = "角色",businessType = BusinessTypeEnum.UPDATE)
	@PostMapping("/update")
	@RequiresPermissions("system:role:edit")
	@ApiOperation(value="修改")
	public Result update(@Validated @RequestBody SysRoleEntity role){
		AssertUtils.isNull(role.getRoleId(),"角色ID不能为空");
		CommonUserVO user = getUser();
		sysRoleService.update(role);
		return writeSuccess();
	}
	
	/**
	 * 删除角色
	 */
	@SysLog(value = "角色",businessType = BusinessTypeEnum.DELETE)
	@PostMapping("/delete")
	@RequiresPermissions("system:role:delete")
	@ApiOperation(value="删除")
	public Result delete(@RequestBody IdsVO idsVO){
		for (Long id: idsVO.getIds()  ) {
			long count = sysRoleService.count(new QueryWrapper<SysRoleEntity>()
					.eq("role_id", id).last(" and init_type in ('I','S')"));
			if (count>0){
				return writeFail("该角色为初始化角色,不能删除");
			}
			long num = sysUserRoleService.count(new QueryWrapper<SysUserRoleEntity>()
					.eq("role_id", id));
			if (num>0){
				return writeFail("该记录已关联用户,不能删除");
			}
		}
		sysRoleService.deleteBatch(idsVO.getIds());
		return writeSuccess();
	}
}
